<?php

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use ZendSearch\Lucene;
use ZendSearch\Lucene\Document;
use ZendSearch\Lucene\Index;

class SearchController extends AbstractActionController
{
    protected $storage;
    protected $authservice;
	
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authservice;
    }

    public function getIndexLocation()
    {
        $config = $this->getServiceLocator()->get('config');
        if ($config instanceof Traversable) {
            $config = ArrayUtils::iteratorToArray($config);
        }
        if (!empty($config['module_config']['search_index'])) {
            return $config['module_config']['search_index'];
        } else {
            return FALSE;
        }
    }
    
    public function indexAction()
    {
        $form = $this->getServiceLocator()->get('formSearch');
        
        $request = $this->getRequest();
        if($request->isPost())
        {
            $queryText = $request->getPost()->get('query');
            $searchIndexLocation = $this->getIndexLocation();
            $index = Lucene\Lucene::open($searchIndexLocation);die(print 'xyz');
            $searchResults = $index->find($queryText);
                        
            var_dump($searchResults);
        }
        
        return new ViewModel(array(
            'form' => $form
        ));
    }


}

